package org.clockwise.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.clockwise.nlpir.TestNLPIR;


public class DocReader {
	
	public static final String DOC_END_SIGN = "</doc>";
	public static final String CONTENT_BEGIN_SIGN = "\"jspreload\">";
	
	private List<String> dirtyDocs = null;
	private List<String> cleanDocs = null;
	
	public DocReader(String filename) {
		dirtyDocs = new ArrayList<String>();
		cleanDocs = new ArrayList<String>();
		File file = new File(filename);
		if (file.exists() && file.isFile()) {
			
			try {
				BufferedReader reader = new BufferedReader(new FileReader(file));
				String temp = null;
				StringBuilder sb = new StringBuilder();
				while ((temp = reader.readLine()) != null) {
					sb.append(temp);
					if (temp.indexOf("</doc>") >= 0) {
						dirtyDocs.add(sb.toString());
						sb = new StringBuilder();
					}
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		for (String doc : dirtyDocs) {
			cleanDocs.add(DocReader.parseDoc(doc));
		}
	}
	
	public DocReader(String input, String output) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(input));
			MyFileWriter mfw = new MyFileWriter(output, false);
			String temp = null;
			StringBuilder sb = new StringBuilder();
			while ((temp = reader.readLine()) != null) {
				sb.append(temp);
				if (!temp.endsWith(">")) {
					sb.append("\n");
				}
				if (temp.indexOf("</doc>") >= 0) {
					String cleanLine = parseDoc(sb.toString());
					if (cleanLine.length() > 200) {
						mfw.write("<doc>\n" + cleanLine + "</doc>\n");
					}
					sb = new StringBuilder();
				}
			}
			reader.close();
			mfw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<String> getCleanDoc() {
		return this.cleanDocs;
	}
	
	public static String parseDoc(String input) {
		String content = null;
		int begin = input.indexOf(DocReader.CONTENT_BEGIN_SIGN);
		begin += DocReader.CONTENT_BEGIN_SIGN.length();
		int end = input.length() - 6;
		
		String temp = input.substring(begin, end);
		int l = temp.indexOf("<"), r = temp.indexOf(">");
		while (l >= 0 && l < r) {
			temp = temp.substring(0, l) + temp.substring(r + 1, temp.length());
			l = temp.indexOf("<");
			r = temp.indexOf(">");
		}
		return temp;
	}
	
}
